package wechatpay

import (
	"context"
	"encoding/json"
	"log"
	"yundian/internel/app/models/paysetting"
	"yundian/internel/pkg/dingo/global"
	"yundian/internel/pkg/utils"
)

// ------------ Api v3 - payConst(相关常量配置) -----------------//

const (
	wxBaseUrl = "https://api.mch.weixin.qq.com"

	// api 请求路径
	wxServiceOrder   = "/v3/payscore/serviceorder"   // 支付分-下订单、查询、完结
	wxCertificates   = "/v3/certificates"            // 获取证书列表
	wxRefunds        = "/v3/refund/domestic/refunds" // 退款API、查询退款
	wxTransactions   = "/v3/pay/transactions/jsapi"        // 统一下订单
	wxQueryPayOrders = "/v3/pay/transactions"
	// 商户证书路径
	wechatPayCert = "./configs/certs/apiclient_cert.pem" // 商户证书公钥 路径
	wechatPayKey  = "./configs/certs/apiclient_key.pem"  // 商户证书私钥 路径
	// 中曼速充证书
	apiclientCert = "./configs/zmwl/apiclient_cert.pem" // 商户证书公钥 路径
	apiclientKey  = "./configs/zmwl/apiclient_key.pem"  // 商户证书私钥 路径

	apiclientCertZl = "./configs/zmzl/apiclient_cert.pem" // 商户证书公钥 路径
	apiclientKeyZl  = "./configs/zmzl/apiclient_key.pem"  // 商户证书私钥 路径

	WechatCert = "./configs/certs/wx_cert.pem" // 微信平台证书 路径

	// 认证类型
	wechatScheme = "WECHATPAY2-SHA256-RSA2048"

	complaintsList       = "/v3/merchant-service/complaints-v2?"          //消费者投诉列表
	complaintsDetails    = "/v3/merchant-service/complaints-v2"           //查询投诉单详情
	createNotifyLogicURL = "/v3/merchant-service/complaint-notifications" //创建投诉通知回调地址
	uploadImage          = "/v3/merchant-service/images/upload"           //商户上传图片

	// 请求头user-agent
	userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1"

	// 加密类型
	SignTypeMD5        = "MD5"
	SignTypeSHA1       = "SHA1"
	SignTypeHMACSHA256 = "HMAC-SHA256"

	// 返回值
	RCSuccess = "SUCCESS"
	RCFail    = "FAIL"
)

// 获取微信支付配置
func GetWxPaySetting() *paysetting.PaySetting {
	settInfo := &paysetting.PaySetting{}
	// 判断redis中是否存在
	s := global.Rdb.Get(context.Background(), utils.Join(global.Cfg.Wx.PayTypeName, "-paysetting")).Val()
	if s == "" {
		//settInfo = dao.NewSiteSettingDao().GetPaySettingInfoByTypeName(helper.WechatName).Unwrap().(*paysetting.PaySetting)
		global.Orm.Table("pay_setting").Where("type_name = ?", global.Cfg.Wx.PayTypeName).Find(&settInfo)
		global.Rdb.Set(context.Background(), utils.Join(global.Cfg.Wx.PayTypeName, "-paysetting"), settInfo, 0)
	} else {
		_ = json.Unmarshal([]byte(s), &settInfo)
	}
	log.Println("打印测试信息---配置-wecatpay", settInfo, settInfo.MchId)
	return settInfo
}
